package com.jz.community.moduleshoppingguide.search.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.bean.baseGoods.BaseGoodsInfo;
import com.jz.community.basecomm.utils.BaseImageLoaderUtils;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshoppingguide.R;

import java.util.List;

/**
 * Created by Tony on 2018/6/05
 * <p>
 * 搜索商品列表
 */
public class SearchGoodsListAdapter extends BaseQuickAdapter<BaseGoodsInfo.EmbeddedBeanXX.ContentBeanX, BaseViewHolder> {

    private GridLayoutManager gridLayoutManager;

    public SearchGoodsListAdapter(int layoutResId, @Nullable List<BaseGoodsInfo.EmbeddedBeanXX.ContentBeanX> data, GridLayoutManager gridLayoutManager) {
        super(layoutResId, data);
        this.gridLayoutManager = gridLayoutManager;
    }

    @Override
    protected void convert(BaseViewHolder helper, BaseGoodsInfo.EmbeddedBeanXX.ContentBeanX entity) {
        int spanCount = gridLayoutManager.getSpanCount();
        RelativeLayout grid_ll = helper.getView(R.id.farmer_grid_ll);
        RelativeLayout linear_ll = helper.getView(R.id.farmer_linear_ll);
        if (spanCount == 1) {
            //Linear
            SHelper.vis(linear_ll);
            SHelper.gone(grid_ll);
            ImageView imageView = helper.getView(R.id.farmer_linear_iv);
            BaseImageLoaderUtils.getInstance().loadDefaltImage(mContext, imageView, entity.getIcon());
            helper.setText(R.id.farmer_linear_name, entity.getTitle());
            helper.setText(R.id.farmer_linear_description, entity.getTitle());
            helper.setText(R.id.farmer_linear_persons, entity.getSaleCount() + "位邻居已购买");
            TextView price_tv = helper.getView(R.id.farmer_linear_price);
            if (!Preconditions.isNullOrEmpty(entity.getDiscountPrice())) {
                price_tv.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(entity.getDiscountPrice())));
            } else {
                price_tv.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(entity.getPrice())));
            }
        } else {
            //Grid
            SHelper.vis(grid_ll);
            SHelper.gone(linear_ll);
            ImageView imageView = helper.getView(R.id.farmer_grid_iv);
            BaseImageLoaderUtils.getInstance().loadDefaltImage(mContext, imageView, entity.getIcon());
            helper.setText(R.id.farmer_grid_persons, entity.getSaleCount() + "位邻居已购买");
            TextView price_tv = helper.getView(R.id.farmer_grid_price);
            if (!Preconditions.isNullOrEmpty(entity.getDiscountPrice())) {
                price_tv.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(entity.getDiscountPrice())));
            } else {
                price_tv.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(entity.getPrice())));
            }
            helper.setText(R.id.farmer_grid_name, entity.getTitle());
        }
    }
}
